package com.hyd.hx_adr.modules.examine.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyd.hx_adr.domain.entity.JsonResult;
import com.hyd.hx_adr.domain.info.BaseBatchInfo;
import com.hyd.hx_adr.domain.info.BaseInfo;
import com.hyd.hx_adr.exception.BusinessException;
import com.hyd.hx_adr.modules.examine.domain.dto.ImportExamineDto;
import com.hyd.hx_adr.modules.examine.domain.entity.Examine;
import com.hyd.hx_adr.modules.examine.mapper.ExamineMapper;
import com.hyd.hx_adr.modules.examine.service.ExamineService;
import com.hyd.hx_adr.utils.ImportExcelUtils;
import com.hyd.hx_adr.utils.ResultUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExamineServiceImpl extends ServiceImpl<ExamineMapper, Examine> implements ExamineService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult<String> importExamine(MultipartFile file) {
        try {
            List<ImportExamineDto> listData = ImportExcelUtils.getListData(file, ImportExamineDto.class, 2);
            List<Examine> examineList = new ArrayList<>();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            listData.forEach(item -> {
                Examine examine = new Examine();
                examine.setAdmissionNumber(item.getAdmissionNumber());
                examine.setCaseNumber(item.getCaseNumber());
                examine.setMedicalNumber(item.getMedicalNumber());
                examine.setItemName(item.getItemName());
                examine.setItemValue(item.getItemValue());
                examine.setItemUnit(item.getItemUnit());
                examine.setReferenceRange(item.getReferenceRange());
                examine.setResultAttribute(item.getResultAttribute());

                try {
                    examine.setSampleTime(simpleDateFormat.parse(item.getSampleTime()));
                }  catch (Exception e) {
                    throw new BusinessException("无法识别的日期，请按照提示的日期格式进行填充!");
                }
                examineList.add(examine);
            });

            saveBatch(examineList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.success();
    }

    @Override
    public JsonResult<List<Examine>> queryExamineList(BaseInfo info) {
        if (null == info.getPage()) info.setPage(1);
        if (null == info.getLimit()) info.setLimit(10);

        LambdaQueryChainWrapper<Examine> wrapper = lambdaQuery();
        wrapper.isNull(Examine::getIsDeleted);
        Page<Examine> page = wrapper.page(new Page<>(info.getPage(), info.getLimit()));
        return ResultUtils.success(page.getRecords(), page.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult<String> deleteExamine(BaseBatchInfo info) {
        lambdaUpdate()
                .in(Examine::getId, info.getIds())
                .set(Examine::getIsDeleted, info.getOwner())
                .update();
        return ResultUtils.success();
    }
}
